package com.bj.dljd.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bj.dljd.domain.UserInfo;
import com.bj.dljd.domain.UserPayConfig;
import com.bj.dljd.domain.UserPayInfo;
import com.bj.dljd.enums.PublicStatus;
import com.bj.dljd.excepyions.BossException;
import com.bj.dljd.mapper.UserInfoMapper;
import com.bj.dljd.mapper.UserPayConfigMapper;
import com.bj.dljd.mapper.UserPayInfoMapper;
import com.bj.dljd.models.query.UserConfigQuery;
import com.bj.dljd.service.UserConfigService;
import com.bj.dljd.util.IdUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserConfigServiceImpl implements UserConfigService {

    @Resource
    private UserPayConfigMapper userPayConfigMapper;

    @Resource
    private UserPayInfoMapper userPayInfoMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public Page<UserPayConfig> productConfigList(Integer pageNo, Integer pageSize) {
        QueryWrapper<UserPayConfig> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("user_no","pay_product_code");

        return userPayConfigMapper.selectPage(Page.of(pageNo,pageSize),wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createUserConfig(UserConfigQuery configQuery) {
        QueryWrapper<UserPayConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no",configQuery.getUserNo())
                .eq("pay_product_code",configQuery.getProductCode())
                ;
        UserPayConfig userPayConfig = userPayConfigMapper.selectOne(wrapper);
        if (userPayConfig != null){
            throw new BossException("用户支付配置已存在");
        }
        // 创建user_pay_config记录
        UserPayConfig userPayConfig1 = new UserPayConfig();
        userPayConfig1.setId(IdUtil.id());
        userPayConfig1.setUserNo(configQuery.getUserNo());
        userPayConfig1.setShopName(configQuery.getShopName());
        userPayConfig1.setPayProductCode(configQuery.getProductCode());
        userPayConfig1.setPayProductName(configQuery.getProductName());
        userPayConfig1.setRiskDay(configQuery.getRiskDay());
        userPayConfig1.setAppKey(IdUtil.uuid());
        userPayConfig1.setAppSecret(IdUtil.uuid());
        userPayConfig1.setMerchantServerIp(configQuery.getIpList());
        userPayConfig1.setAutoSett("autoYes".equals(configQuery.getAutoSett()) ? "YES":"NO");
        userPayConfig1.setRemark("创建支付配置");
        userPayConfig1.setEditTime(new Date());
        userPayConfig1.setStatus(PublicStatus.ENABLE.name());
        userPayConfigMapper.insert(userPayConfig1);
        // 添加user_pay_info 第三方支付参数
        UserConfigQuery.KqConfig kqConfig = configQuery.getKqConfig();
        if (kqConfig != null && StrUtil.isNotBlank(kqConfig.getMerchId()) &&
            StrUtil.isNotBlank(kqConfig.getPrivateFile())
            && StrUtil.isNotBlank(kqConfig.getPublicFile())
            && StrUtil.isNotBlank(kqConfig.getQueryKey())
        ){
            UserPayInfo userPayInfo = getUserPayInfo(configQuery, kqConfig);
            userPayInfoMapper.insert(userPayInfo);
        }
        UserConfigQuery.WxConfig wxConfig = configQuery.getWxConfig();
        if (wxConfig != null && StrUtil.isNotBlank(wxConfig.getAppId()) &&
            StrUtil.isNotBlank(wxConfig.getMerchId())
        ){
            UserPayInfo userPayInfo = getUserPayInfo(configQuery, wxConfig);
            userPayInfo.setStatus(PublicStatus.ENABLE.name());
            userPayInfoMapper.insert(userPayInfo);
        }
    }

    @Override
    public List<UserPayInfo> payInfoList(String userNo, String productCode) {
        QueryWrapper<UserPayInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no",userNo)
                .eq("pay_product_code",productCode)
                .orderByAsc("pay_product_code");
        return userPayInfoMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeConfig(String userNo, String productCode) {
        QueryWrapper<UserPayConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no",userNo)
                .eq("pay_product_code",productCode);
        int delete = userPayConfigMapper.delete(wrapper);
        if (delete > 0){
            QueryWrapper<UserPayInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("user_no",userNo)
                    .eq("pay_product_code",productCode);
            userPayInfoMapper.delete(wrapper1);
        }
    }


    private static UserPayInfo getUserPayInfo(UserConfigQuery configQuery, UserConfigQuery.KqConfig kqConfig) {
        UserPayInfo userPayInfo = new UserPayInfo();
        userPayInfo.setId(IdUtil.id());
        userPayInfo.setPayProductCode(configQuery.getProductCode());
        userPayInfo.setUserNo(configQuery.getUserNo());
        userPayInfo.setShopName(configQuery.getShopName());
        userPayInfo.setPayWayCode("KUAIQIAN");
        userPayInfo.setPayWayName("快钱");
        userPayInfo.setPayConfig(JSONUtil.toJsonStr(kqConfig));// 将配置信息转为json格式
        userPayInfo.setRemark("创建快钱支付配置");
        userPayInfo.setEditTime(new Date());
        userPayInfo.setStatus(PublicStatus.ENABLE.name());
        return userPayInfo;
    }
    private static UserPayInfo getUserPayInfo(UserConfigQuery configQuery, UserConfigQuery.WxConfig kqConfig) {
        UserPayInfo userPayInfo = new UserPayInfo();
        userPayInfo.setId(IdUtil.id());
        userPayInfo.setPayProductCode(configQuery.getProductCode());
        userPayInfo.setUserNo(configQuery.getUserNo());
        userPayInfo.setShopName(configQuery.getShopName());
        userPayInfo.setPayWayCode("WEIXIN");
        userPayInfo.setPayWayName("微信");
        userPayInfo.setPayConfig(JSONUtil.toJsonStr(kqConfig));// 将配置信息转为json格式
        userPayInfo.setRemark("创建微信支付配置");
        userPayInfo.setEditTime(new Date());
        userPayInfo.setStatus(PublicStatus.ENABLE.name());
        return userPayInfo;
    }
}
